package com.splant.android.smart.SystemDefinedLevel.FilesManager;

/**
 * Created by Administrator on 2015/11/1.
 */
import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileHelper {

    private Context context;
    public FileHelper(Context context) {
        this.context = context;
    }
    public static boolean isExistFile(String fileSrc)
    {
        try
        {
            File f=new File(fileSrc);
            if(f.exists()&&f.isFile())
            {
                return true;
            }
            else
            {
               return false;
            }
        }
        catch (Exception e)
        {

            return false;
        }
    }
    public static boolean isExistCreateFile(String fileSrc)
    {
        try
        {
            File f=new File(fileSrc);
            if(!f.exists())
            {
                return f.createNewFile();
            }
            else
            {
                if (f.isFile())
                    return true;
                else
                {
                    return f.createNewFile();
                }
            }
        }
        catch (Exception e)
        {

            return false;
        }
    }
    public static List<String> getDirFilesName(String filePath,String fileSufName_lowerCase,boolean haveSuffNmae)
    {
        List<String> stringList=new ArrayList<>();
        File file=new File(filePath);

        String fileSufName_UpperCase=fileSufName_lowerCase.toUpperCase();
        if (file.isDirectory())
        {
            String[] arrStr=file.list();
            for (String s : arrStr)
            {
                if (s.endsWith(fileSufName_lowerCase)||s.endsWith(fileSufName_UpperCase))
                {
                    if (haveSuffNmae==false)
                        s=s.substring(0,s.lastIndexOf("."));
                    stringList.add(s);
                }
            }
            return stringList;
        }
        else
            return null;
    }
    public static List<String> getDirFilesSrc(String filePath,String fileSufName)
    {
        List<String> stringList=new ArrayList<>();
        File file=new File(filePath);

        String fileSufNamedx=fileSufName.toUpperCase();
        if (file.isDirectory())
        {
            File[] arrFile=file.listFiles();
            for (File f : arrFile)
            {
                String s=f.getPath();
                if (s.endsWith(fileSufName)||s.endsWith(fileSufNamedx))
                {
                    stringList.add(s);
                }
            }
            return stringList;
        }
        else
            return null;
    }
    public static boolean createPath(String path)
    {
        File file = new File(path);
        return file.mkdirs();
    }
    public static boolean isExistCreatePath(String path)
    {
            File file = new File(path);
              if (!file.exists())//判断文件夹是否存在,如果不存在则创建文件夹
              {
               return file.mkdirs();
              }
              else
                  return true;
    }
    public static boolean isExistPath(String path)
    {
        File file = new File(path);
        if (file.exists()&&file.isDirectory())//判断文件夹是否存在,如果不存在则创建文件夹
        {
           return true;
        }
        else
            return false;
    }
    public boolean createFile(String fileSrc) throws IOException {
        File file = new File(fileSrc);
        return file.createNewFile();
    }

    public static boolean delFile(String fileSrc) {
        File file = new File(fileSrc);
        if (file == null || !file.exists() || file.isDirectory())
            return false;
        file.delete();
        return true;
    }
    public File creatSDDir(String dirPath,String dirName) {
        File dir = new File(dirPath +"\\"+ dirName);
        dir.mkdir();
        return dir;
    }
    public static boolean changeFileName(String oldFileNam,String newFileName)
    {
        File file=new File(oldFileNam);
        if (file.exists())
        {
           return file.renameTo(new File(newFileName));
        }
        else
            return false;
    }
    public boolean delSDDir(String dirPath) {
        File dir = new File(dirPath);
        return delDir(dir);
    }
    public boolean renameSDFile(String filePath,String oldfileName, String newFileName) {
        File oleFile = new File(filePath+"\\" + oldfileName);
        File newFile = new File(filePath+"\\" + newFileName);
        return oleFile.renameTo(newFile);
    }
    public boolean copySDFileTo(String filePath,String srcFileName, String destFileName)throws IOException {
        File srcFile = new File(filePath + srcFileName);
        File destFile = new File(filePath + destFileName);
        return copyFileTo(srcFile, destFile);
    }


//    public boolean copySDFilesTo(String srcDirName, String destDirName)
//            throws IOException {
//        File srcDir = new File(SDPATH + srcDirName);
//        File destDir = new File(SDPATH + destDirName);
//        return copyFilesTo(srcDir, destDir);
//    }


    public boolean moveSDFileTo(String srcFilePath,String destFilePath,String srcFileName, String destFileName)
            throws IOException {
        File srcFile = new File(srcFilePath+"\\"+ srcFileName);
        File destFile = new File(destFilePath+"\\" + destFileName);
        return moveFileTo(srcFile, destFile);
    }

//
//    public boolean moveSDFilesTo(String srcDirName, String destDirName)
//            throws IOException {
//        File srcDir = new File(SDPATH + srcDirName);
//        File destDir = new File(SDPATH + destDirName);
//        return moveFilesTo(srcDir, destDir);
//    }



//    public boolean copyDataFilesTo(String srcDirName, String destDirName)
//            throws IOException {
//        File srcDir = new File(FILESPATH + srcDirName);
//        File destDir = new File(FILESPATH + destDirName);
//        return copyFilesTo(srcDir, destDir);
//    }



//    public boolean moveDataFilesTo(String srcDirName, String destDirName)
//            throws IOException {
//        File srcDir = new File(FILESPATH + srcDirName);
//        File destDir = new File(FILESPATH + destDirName);
//        return moveFilesTo(srcDir, destDir);
//    }


    public boolean delFile(File file) {
        if (file.isDirectory())
            return false;
        return file.delete();
    }


    public boolean delDir(File dir) {
        if (dir == null || !dir.exists() || dir.isFile()) {
            return false;
        }
        for (File file : dir.listFiles()) {
            if (file.isFile()) {
                file.delete();
            } else if (file.isDirectory()) {
                delDir(file);// 递归
            }
        }
        dir.delete();
        return true;
    }


    public boolean copyFileTo(File srcFile, File destFile) throws IOException {
        if (srcFile.isDirectory() || destFile.isDirectory())
            return false;// 判断是否是文件
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        int readLen = 0;
        byte[] buf = new byte[1024];
        while ((readLen = fis.read(buf)) != -1) {
            fos.write(buf, 0, readLen);
        }
        fos.flush();
        fos.close();
        fis.close();
        return true;
    }
    public boolean moveFileTo(File srcFile, File destFile) throws IOException {
        boolean iscopy = copyFileTo(srcFile, destFile);
        if (!iscopy)
            return false;
        delFile(srcFile);
        return true;
    }

//    public boolean copyFilesTo(File srcDir, File destDir) throws IOException {
//        if (!srcDir.isDirectory() || !destDir.isDirectory())
//            return false;// 判断是否是目录
//        if (!destDir.exists())
//            return false;// 判断目标目录是否存在
//        File[] srcFiles = srcDir.listFiles();
//        for (int i = 0; i &lt; srcFiles.length; i++) {
//            if (srcFiles[i].isFile()) {
//                // 获得目标文件
//                File destFile = new File(destDir.getPath() + "\"
//                        + srcFiles[i].getName());
//                copyFileTo(srcFiles[i], destFile);
//            } else if (srcFiles[i].isDirectory()) {
//                File theDestDir = new File(destDir.getPath() + "\"
//                        + srcFiles[i].getName());
//                copyFilesTo(srcFiles[i], theDestDir);
//            }
//        }
//        return true;
//    }





//    public boolean moveFilesTo(File srcDir, File destDir) throws IOException {
//        if (!srcDir.isDirectory() || !destDir.isDirectory()) {
//            return false;
//        }
//        File[] srcDirFiles = srcDir.listFiles();
//        for (int i = 0; i &lt; srcDirFiles.length; i++) {
//            if (srcDirFiles[i].isFile()) {
//                File oneDestFile = new File(destDir.getPath() + "\"
//                        + srcDirFiles[i].getName());
//                moveFileTo(srcDirFiles[i], oneDestFile);
//                delFile(srcDirFiles[i]);
//            } else if (srcDirFiles[i].isDirectory()) {
//                File oneDestFile = new File(destDir.getPath() + "\"
//                        + srcDirFiles[i].getName());
//                moveFilesTo(srcDirFiles[i], oneDestFile);
//                delDir(srcDirFiles[i]);
//            }
//
//        }
//        return true;
//    }
}